package com.chaos.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.chaos.generator.config.DatasourceConfig;
import com.chaos.generator.config.GeneratorConfig;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeGenerator {

    public static void main(String[] args) {
        List<String> tableNames = Arrays.asList(GeneratorConfig.tables);
        List<String> ignorePrefix = Arrays.asList(GeneratorConfig.ignorePrefix);

        FastAutoGenerator.create(DatasourceConfig.url, DatasourceConfig.username, DatasourceConfig.password)
                .globalConfig(builder -> {
                    builder.author(GeneratorConfig.author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(GeneratorConfig.projectPackagePath); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder
                                .parent(GeneratorConfig.projectPackageName) // 设置父包名
                                .moduleName(GeneratorConfig.project.getName()) // 设置父包模块名
                                .pathInfo(Collections.singletonMap(
                                        OutputFile.xml,
                                        GeneratorConfig.projectMapperPath)) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude(String.join(",", tableNames)) // 设置需要生成的表名
                                .addTablePrefix(ignorePrefix) // 设置过滤表前缀

                                .entityBuilder()
                                .disable()
                                .mapperBuilder()
                                .mapperTemplate("/templates/mapper.java")
                                .mapperXmlTemplate("/templates/mapper.xml")

                                .serviceBuilder()
                                .disableService()
                                .disableServiceImpl()

                                .controllerBuilder()
                                .template("/templates/controller.java")

                                .enableFileOverride() // 覆盖已生成文件

                )
                .injectionConfig(injectConfig -> {
                    Map<String, Object> customMap = new HashMap<>();
                    customMap.put("rootPath", GeneratorConfig.rootPath);
                    customMap.put("projectName", GeneratorConfig.project.getName());

                    injectConfig.customMap(customMap); //注入自定义属性

                    injectConfig.customFile(new CustomFile.Builder()
                            .fileName("Data.java") //文件名称
                            .templatePath("templates/data.java.ftl") //指定生成模板路径
                            .packageName("data") //包名,自3.5.10开始,可通过在package里面获取自定义包全路径,低版本下无法获取,示例:package.entityDTO
                            .build());

                    injectConfig.customFile(new CustomFile.Builder()
                            .fileName(".java")
                            .templatePath("templates/model.java.ftl")
                            .packageName("entity")
                            .build());
                    injectConfig.customFile(new CustomFile.Builder()
                            .fileName("Service.java")
                            .templatePath("templates/service.java.ftl")
                            .packageName("service")
                            .build());

                })

                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


}
